<?php

namespace backend\modules\user\controllers;

use backend\extensions\ApiBaseController;
use backend\services\recharge\RechargeConfigService;
use backend\services\recharge\RechargeRecordService;

class RechargeController extends ApiBaseController
{
    
    /**
     * 充值配置信息
     * * */
    public function actionConfig()
    {
        $params = \Yii::$app->request->post();
        $rules = [
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return RechargeConfigService::detail();
    }
    
    /**
     * 充值配置信息保存-config = [['amount'=>'金额','type'=>'类型-1:送金额,2:送券,暂时默认1','gift'=>'赠送的金额/赠送的券ID-暂时默认送金额']]
     * * */
    public function actionConfigSave()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['config'], 'required', 'message' => '{attribute}属必填项'],
            [['id'], 'default', 'value' => 0, 'message' => '设默认值'],
            [['data'], 'default', 'value' => '', 'message' => '可不填参数设置'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $result = RechargeConfigService::detail();
        if($result['data']){
            $this->params['id'] = $result['data']['id'];
        }
        $this->params['config'] = json_encode($params['config'],320);
        if ($this->params['id']) {
            return RechargeConfigService::update($this->params);
        }
        unset($this->params['id']);
        return RechargeConfigService::create($this->params);
    }
    
    /**
     * 余额交易记录
     * * */
    public function actionRecord()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [['id'], 'required','message' => '会员ID'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 10000, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = RechargeRecordService::getList($this->params);
        return $this->jsonSuccess($data);
    }
    
    /**
     * 余额调整
     * * */
    public function actionRecharge()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [['id','store'], 'required','message' => '会员ID|门店ID'],
            [['type'], 'required','message' => '类型-[1:增加,2:减少]'],
            [['amount'], 'required','message' => '金额'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = RechargeRecordService::recharge($this->params);
        return $this->jsonSuccess($data);
    }
}
